/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jabxlab;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 *
 * @author k00112173
 */
public class JabxLab {

    /**
     * @param args the command line arguments
     */
    private static final String CATALOG_XML_IN = "./cd_catalog.xml";
    private static final String CATALOG_XML_OUT = "./cd_catalog2.xml";
    
    public static void main(String[] args) throws JAXBException, IOException {
        
        int option;
            do{
                
                Scanner input = new Scanner(System.in);

                System.out.println("1.  Add CD");
                System.out.println("2.  Display CDs");
                System.out.println("0.  Exit");
                System.out.println("Enter Option:");
                
                option = input.nextInt();


                    switch(option){
                        case 0:
                            break;
                        case 1:
                                writeCD();
                            break;
                        case 2:
                                readCD();
                    
                            break;
                       
                        default:
                            System.out.println("not a valid option");
                            break;
                    }
                }while(option!=0);
    }
        
    
        private static void writeCD()throws JAXBException, IOException{
            
           
        // TODO code application logic here
        cd cd1 = new cd();
        cd1.setTITLE("Transformer");
        cd1.setARTIST("Lou Reed");
        cd1.setCOUNTRY("USA");
        cd1.setYEAR("1987");
        cd1.setCOMPANY("Vista");
        cd1.setPRICE("14.99");
        
        ArrayList<cd> cdList = new ArrayList<cd>();
        cdList.add(cd1);
        CdCatalog x = new CdCatalog();
        x.setCdList(cdList);
        
        
        JAXBContext context = JAXBContext.newInstance(CdCatalog.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(x, System.out);
        
        Writer w = null;
        try{
            w = new FileWriter(CATALOG_XML_OUT);
            m.marshal(x, w);
            }
        finally{
            try{
                w.close();
            } catch (Exception e){
                
            }
        }
    }

    private static void readCD()throws JAXBException, IOException {
        
        JAXBContext context = JAXBContext.newInstance(CdCatalog.class);
        System.out.println("The CDs in the catalog are:\n");
        Unmarshaller um = context.createUnmarshaller();
        
        CdCatalog cdlist = (CdCatalog) um.unmarshal((new FileReader(CATALOG_XML_IN)));
        
            for(int i = 0; i< cdlist.getCdLists().toArray().length; i++){
                System.out.println("CD " + (i + 1) + ":\n"
                        + "Title: " +cdlist.getCdLists().get(i).getTITLE() + "\n" 
                        + "Artist: " +cdlist.getCdLists().get(i).getARTIST() + "\n"
                        + "Price: " +cdlist.getCdLists().get(i).getPRICE() + "\n"
                        + "Country: " +cdlist.getCdLists().get(i).getCOUNTRY() + "\n"
                        + "Year: " +cdlist.getCdLists().get(i).getYEAR() + "\n");
            }
        
        
    }
    
}
